SPSC: a Simple Supercompiler in Scala
نویسندگان
چکیده
class Term case class Var(name: String) extends Term case class CFG(kind: TKind.Value, name: String, args: List[Term]) extends Term { def replaceArgs(newArgs: List[Term]) = CFG(kind, name, newArgs) } case class Let(term: Term, bindings: List[(Var, Term)]) extends Term case class Pat(name: String, args: List[Var]) object TKind extends Enumeration {val Ctr, FCall, GCall = Value} class CFGObject(kind: TKind.Value) extends ((String, List[Term]) => CFG) { def apply(name: String, args: List[Term]) = CFG(kind, name, args) def unapply(e: CFG) = if (e.kind == kind) Some(e.name, e.args) else None } object Ctr extends CFGObject(TKind.Ctr) object FCall extends CFGObject(TKind.FCall) object GCall extends CFGObject(TKind.GCall) abstract class Def {def name: String} case class FFun(name: String, args: List[Var], term: Term) extends Def case class GFun(name: String, p: Pat, args: List[Var], term: Term) extends Defclass Def {def name: String} case class FFun(name: String, args: List[Var], term: Term) extends Def case class GFun(name: String, p: Pat, args: List[Var], term: Term) extends Def case class Program(defs: List[Def]){ val f = (defs :\ (Map[String, FFun]())) {case (x: FFun, m) => m + (x.name -> x); case (_, m) => m} val g = (defs :\ (Map[(String, String), GFun]())) {case (x: GFun, m) => m + ((x.name, x.p.name) -> x); case (_, m) => m} val gs = (defs :\ Map[String, List[GFun]]().withDefaultValue(Nil)) {case (x: GFun, m) => m + (x.name -> (x :: m(x.name))); case (_, m) => m} } Fig. 2. SLanguage.scala To simplify manipulations with terms we define functional objects Ctr, FCall, GCall. A method apply is only used to mimick a constructor: c = Ctr(n, ts) expands to c = Ctr.apply(n, ts). A method named unapply is used for generalized pattern-matching yielding an extractor : the pattern case Ctr(n, ts) will cause an invocation of Ctr.unapply. Some(n, args) corresponds to a successful match, while None is a failure. In addition to the list of definitions defs (declared in the constructor), the class Program contains three value fields: f, g, and gs. f is a map, where key is a name of an f-function and a value is the f-function itself. g is a map which uses a pair (a g-function name, a pattern name) as a key and the corresponding g-function as a value. gs is a map with a g-function name as a key and a list of g-functions as a value. In Scala, a piece of code appearing inside a class, but outside the method definitions, is considered to be a part of the constructor body. So, in the class Program, the maps f, g and gs are initialized when the constructor is called. To populate the map f, we traverse the list of function definitions. If an f-function is encountered then it is added to f. Note that this can be written in Scala in an elegant way by means of higher-order functions. We use the method :\ of the class List (where :\ is an alias for foldRight) possessing the following property: ([a0, a1, a2 .. an] :\ z) op = op(a0, op(a1, op(..., op(an, z) ...)))
منابع مشابه
Implementing a domain-specific multi-result supercompiler by means of the MRSC toolkit (2012)
The paper presents a simple domain-specific multi-result supercompiler for counter systems implemented by means of the MRSC toolkit. The input language of the supercompiler is a non-deterministic domain-specific language meant for specifying models of communication protocols. The implementation of this DSL is based on " embedding " and the heavy use of higher-order constructs. There are present...
متن کاملComparison of standardized peristomal skin care and crusting technique in prevention of peristomal skin problems in ostomy patients.
PURPOSE This study was performed to compare the effects of standardized peristomal skin care (SPSC) and crusting technique (CT) on the peristomal skin of ostomates. SPSC was developed by a consensus among the expert group based on a comprehensive review of the relevant literature and hospital protocols. METHODS A randomized controlled pilot trial with 2 parallel arms was used. A total of 81 o...
متن کاملSingle-Consumer Queues on Shared Cache Multi-Core Systems
Using efficient point-to-point communication channels is critical for implementing fine grained parallel program on modern shared cache multicore architectures. This report discusses in detail several implementations of wait-free Single-Producer/Single-Consumer queue (SPSC), and presents a novel and efficient algorithm for the implementation of an unbounded wait-free SPSC queue (uSPSC). The cor...
متن کاملProgram Transformation with Metasystem Transitions: Experiments with a Supercompiler
Turchin's supercompilationis a program transformation technique for functional languages. A supercompiler is a program which can perform a deep transformation of programs using a principle which is similar to partial evaluation. In the present paper we use a supercompiler, which V.F. Turchin and we have described in 22], 23]. The aim of our investigation has been to show, what deep changes (w.r...
متن کاملA Characterization of the Single-Peaked Single-Crossing Domain
We investigate elections that are simultaneously singlepeaked and single-crossing (SPSC). We show that the domain of 1-dimensional Euclidean elections (where voters and candidates are points on the real line, and each voter prefers the candidates that are close to her to the ones that are further away) is a proper subdomain of the SPSC domain, by constructing an election that is single-peaked a...
متن کامل